<!--
*  功能描述：项目立项
 -->
<template>
  <div style="width: 1000px; margin: 0 auto">
    
    <el-tag style="width: 100%; text-align: left; font-size: 20px"
      >项目录入</el-tag
    >
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      style="text-align: left"
    >
      <el-row>
          <el-col :span="12">
          <el-form-item label="项目序号" prop="ProjectId">
            <el-input v-model="ruleForm.ProjectId" style="width: 220px"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目名称" prop="ProjectName">
            <el-input v-model="ruleForm.ProjectName" style="width: 220px"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="管理级别" prop="ProjectLevel">
            <el-radio-group v-model="ruleForm.ProjectLevel">
              <el-radio label="院管"></el-radio>
              <el-radio label="所管"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="建筑类别" prop="ProjectBuildtype">
            <el-select v-model="ruleForm.ProjectBuildtype" placeholder="请选择">
              <el-option label="特级" value="特级"></el-option>
              <el-option label="一级" value="一级"></el-option>
              <el-option label="二级" value="二级"></el-option>
              <el-option label="三级" value="三级"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="设计阶段" prop="ProjectStage">
            <el-checkbox-group v-model="ruleForm.ProjectStage" style="float: left">
              <el-checkbox label="方案设计" name="ProjectStage"></el-checkbox>
              <el-checkbox label="初步设计" name="ProjectStage"></el-checkbox>
              <el-checkbox label="施工图设计" name="ProjectStage"></el-checkbox>
              <el-checkbox label="其他" name="ProjectStage"></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="合同额(万元)" prop="ProjectPrice">
            <el-input
              type="number"
              v-model="ruleForm.ProjectPrice"
              style="width: 220px"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="建设规模" prop="ProjectScale">
            <el-input
              type="number"
              v-model="ruleForm.ProjectScale"
              style="width: 220px"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="开始时间">
            <el-date-picker
              v-model="ruleForm.ProjectStartTime"
              type="date"
              placeholder="选择日期"
              style="float: left"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间">
            <el-date-picker
              v-model="ruleForm.ProjectEndTime"
              type="date"
              placeholder="选择日期"
              style="float: left"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="电话" prop="ProjectPhone">
            <el-input v-model="ruleForm.ProjectPhone" style="width: 220px"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="承接部门" prop="ProjectDeptID">
            <el-select v-model="ruleForm.ProjectDeptID" placeholder="请选择">
              <el-option
                v-for="item in deptdata"
                :key="item.DepId"
                :value="item.DepId"
                :label="item.DepName"
                >{{ item.DepName }}</el-option
              ></el-select
            >
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="甲方联系人" prop="ProjectRId">
            <el-select v-model="ruleForm.ProjectRId" placeholder="请选择">
              <el-option
                v-for="item in Resxl"
                :key="item.PersonId"
                :value="item.PersonId"
                :label="item.PersonNmae"
                >{{ item.PersonNmae }}</el-option
              >
            </el-select>
            
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同关联" prop="ProjectConId">
            <el-select v-model="ruleForm.ProjectConId" placeholder="请选择">
              <el-option
                v-for="item in data"
                :key="item.ContractId"
                :value="item.ContractId"
                :label="item.ContractName"
                >{{ item.ContractName }}</el-option
              >
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目高清图" prop="">
          <el-upload
  class="avatar-uploader"
  action="Operation/UpLoad"
  :show-file-list="false"
  :on-success="handleAvatarSuccess"
  :before-upload="beforeAvatarUpload">
  <img v-if="ruleForm.ProjectImage" :src="ruleForm.ProjectImage" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</el-form-item>
        </el-col>
      </el-row>
      <el-col :span="24" style="text-align: center;">
        <el-form-item>
          <el-button type="primary" @click="Add('ruleForm')"
            >立即创建</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-col>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ProjectImage:'',
      dialogImageUrl: '',
        dialogVisible: false,
        disabled: false,
      deptdata: [],
      data: [],
      Resxl:[],
      ruleForm: {
        ProjectId: "",
        ProjectName: "",
        ProjectLevel: "院管",
        ProjectBuildtype: "",
        ProjectStage: [],
        ProjectPrice: null,
        ProjectScale: null,
        ProjectStartTime: "",
        ProjectEndTime: "",
        ProjectPhone: "",
        ProjectImage:'',
        ProjectDeptID: "",
        ProjectRId: null,
        ProjectConId: "",
      },
      rules: {
        ProjectName: [{ required: true, message: "请输入项目名称", trigger: "blur" }],
        ProjectLevel: [
          { required: true, message: "请选择管理级别", trigger: "change" },
        ],
        ProjectBuildtype: [
          { required: true, message: "请选择建设类型", trigger: "change" },
        ],
        ProjectPrice: [{ required: true, message: "请输入合同额", trigger: "blur" }],
        ProjectScale: [{ required: true, message: "请输入建设规模", trigger: "blur" }],
        ProjectStartTime: [
          {
            type: "date",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        ProjectEndTime: [
          {
            type: "date",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        ProjectPhone: [{ required: true, message: "请输入电话号码", trigger: "blur" }],
         
      },
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
        this.ruleForm.ProjectImage = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
    handleRemove(file) {
        console.log(file);
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      handleDownload(file) {
        console.log(file);
      },
    Add(formName) {
       var P = /^1[3-9]\d{9}$/;
       if(!P.test(this.ruleForm.ProjectPhone)){
         alert("电话格式不正确");
         return;
       }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.ProjectStage = this.ruleForm.ProjectStage.toString();
          console.log(this.ruleForm);
          this.$http({
            url: "Operation/AddProject",
            method: "Post",
            data: this.ruleForm,
          }).then((res) => {
            if (res.data > 0) {
              console.log(res.data);
              this.$message.success("项目创建成功");
              this.$router.push("/ShowProject");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    /*Add() {
      this.ruleForm.stage = this.ruleForm.stage.toString();
      console.log(this.ruleForm);
      this.$axios({
        url: "http://localhost:49191/api/Operation/AddProject",
        method: "Post",
        data: this.ruleForm,
      }).then((res) => {
        if (res.data > 0) {
          console.log(res.data);
          this.$message.success("项目创建成功");
          this.$router.push("/ShowProject");
        }
      });
    },*/
    DeptXl() {
      this.$http({
        url: "Operation/GetDept",
        method: "get",
      }).then((res) => {
        this.deptdata = res.data;
        console.log(this.data);
      });
    },
    Rexl(){
      this.$http({
        url: "Operation/ShowReponsble",
        method: "get",
      }).then((res) => {
        this.Resxl = res.data;
        console.log(this.data);
      });
    },
    Xl() {
      this.$http({
        url: "Operation/SelCon",
        method: "Get",
      }).then((res) => {
        this.data = res.data;
        console.log(this.data);
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  created() {
    this.Xl();
    this.DeptXl();
    this.Rexl();
  },
};
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>